import request from '@/utils/request';
import {download, toFormData, checkDownloadRes} from '@/utils/common';

/**
 * 分页查询分类信息
 */
export async function pageTypes(params) {
  const res = await request.get('/admin/cms/type/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询分类下拉列表
 * @returns {Promise<any>}
 */
export async function dropDownListTypes() {
  const res = await request.get('/admin/cms/type/dropDownList');
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加分类信息
 */
export async function addType(data) {
  const res = await request.post('/admin/cms/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改分类信息
 */
export async function updateType(data) {
  const res = await request.put('/admin/cms/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除分类信息
 */
export async function removeType(id) {
  const res = await request.delete('/admin/cms/type/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除分类信息
 */
export async function removeTypes(ids) {
  const res = await request.delete('/admin/cms/type/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出分类信息
 */
export async function exportTypes(params) {
  const res = await request({
    url: '/admin/cms/type/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `type_${Date.now()}.xlsx`);
}
